const getTime = () => {
    //获取当前时间
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    if (month < 10) month = '0' + month;
    if (day < 10) day = '0' + day;
    let nowDate = `${year}-${month}-${day}`;
    return nowDate;
}
// 现在日期所在的星期日期范围
const getDayIsWeek = () => {
    let now = new Date();
    let nowTime = now.getTime();
    let day = now.getDay();
    let oneDayTime = 24 * 60 * 60 * 1000;
    // 本周周一
    let weekStartTime = nowTime - (day - 1) * oneDayTime;
    // 本周周日
    let weekEndTime = nowTime + (7 - day) * oneDayTime;
    let weekStartDate = new Date(weekStartTime);
    let weekEndDate = new Date(weekEndTime);
    let startMonth = weekStartDate.getMonth() + 1;
    let startDate = weekStartDate.getDate();
    let endMonth = weekEndDate.getMonth() + 1;
    let endDate = weekEndDate.getDate();
    let year = now.getFullYear();
    if (endMonth < 10) endMonth = '0' + endMonth;
    if (endDate < 10) endDate = '0' + endDate;
    if (startMonth < 10) startMonth = '0' + startMonth;
    if (startDate < 10) startDate = '0' + startDate;
    let weekTime = [`${year}${startMonth}${startDate}`, `${year}${endMonth}${endDate}`];
    return weekTime;
}

// 获取当前时间所在月的第一天和最后一天
const getMonth = (type, months) => {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    month = month < 10 ? "0" + month : month;
    var date = d.getDate();
    var firstday = year + month + "01";
    var lastday = "";
    if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month == "12") {
        lastday = year + month + 31;
    } else if (month == "02") {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
            lastday = year + month + 29;
        } else {
            lastday = year + month + 28;
        };
    } else {
        lastday = year + month + 30;
    };
    return [firstday, lastday];
}
//获取当前年份起止日期
const getYear=()=>{
    var d = new Date();
    var year = d.getFullYear();
    return [`${year}0101`,`${year}1231`];
}
export { getTime, getDayIsWeek, getMonth,getYear }


